package monster.com.cvh.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.GetRequest;

import java.util.ArrayList;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.activity.ResumeTemplateShopActivity;
import monster.com.cvh.adapter.ResumeTemplatePreviewAdapter;
import monster.com.cvh.bean.ResumeSmallBean;
import monster.com.cvh.bean.ResumeTemplateBean;
import monster.com.cvh.database.GreenDAOManager;
import monster.com.cvh.database.ResumeInfoBean;
import monster.com.cvh.gen.ResumeInfoBeanDao;
import monster.com.cvh.listener.ItemClickListener;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:简历模板小图预览PopupWindow
 */

public class ResumePopupWindow extends PopupWindow {
    private static final String TAG = "ResumePopupWindow";
    private View mMenuView;
    private Context mContext;
    private ItemClickListener mItemClickListener;
    private RecyclerView mRecyclerView;
    private List<ResumeTemplateBean> mData = new ArrayList<>();
    private ResumeTemplatePreviewAdapter mAdapter;

    public ResumePopupWindow(Context context, ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
        this.mContext = context;
        initData(context);

    }

    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_mould, null);
        mRecyclerView = (RecyclerView) mMenuView.findViewById(R.id.resume_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new ResumeTemplatePreviewAdapter(mData, mItemClickListener);
        View footer = LayoutInflater.from(context).inflate(R.layout.item_resueme_footer, null, false);
        footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ResumeTemplateShopActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
        mAdapter.addFooterView(footer, 1, LinearLayout.HORIZONTAL);
        mRecyclerView.setAdapter(mAdapter);

        //手指在外抬起时，隐藏弹窗菜单
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.ll).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.DialogAnimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    private void initData(final Context context) {
        String token = SPUtils.getString(context, MyConstant.TOKEN, "");
        GetRequest request = OkGo.<String>get(MyConstant.GET_DEFAULT_TEMPLATES)
                .tag(this);
        if (!token.isEmpty()) {
            request.params(MyConstant.TOKEN, token);
        }
        request.execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    ResumeSmallBean bean = new Gson().fromJson(s, ResumeSmallBean.class);
                    if (bean != null && bean.getCode() == 1) {
                        //插入网络获取的模板预览
                        List<ResumeSmallBean.DataBean> data = bean.getData();
                        for (ResumeSmallBean.DataBean dataBean : data) {
                            mData.add(new ResumeTemplateBean(dataBean.getId(), dataBean.isIs_lock(), dataBean.getCover_image_url()));
                        }
                        //插入本地下载的模板
                        final ResumeInfoBeanDao dao = GreenDAOManager.getInstance().getmDaoSession().getResumeInfoBeanDao();
                        List<ResumeInfoBean> list = dao.queryBuilder().list();
                        for (ResumeInfoBean resumeInfoBean : list) {
                            mData.add(0, new ResumeTemplateBean(resumeInfoBean.getResumeId(), resumeInfoBean.getIsLock(), resumeInfoBean.getFilePath()));
                        }
                        initView(context);
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void setNewDate(List<ResumeTemplateBean> date) {
        if (mAdapter != null) {
            mAdapter.addNewResume(date);
            mRecyclerView.smoothScrollToPosition(0);
        }
    }

    public void refreshData(){
        if (mData==null||mData.size()==0){
            initData(mContext);
        }
    }

}
